#!/usr/bin/env node

/**
 * 入口命名测试脚本
 * 演示 entry 命名的作用和效果
 */

const fs = require('fs');
const path = require('path');

console.log('🧪 Entry 入口命名测试\n');

// 读取当前配置
const tsupConfigPath = path.join(__dirname, '../packages/ui/tsup.config.ts');
const packageJsonPath = path.join(__dirname, '../packages/ui/package.json');

console.log('📋 当前配置分析:');

// 解析 tsup 配置
if (fs.existsSync(tsupConfigPath)) {
  const configContent = fs.readFileSync(tsupConfigPath, 'utf8');
  console.log('\n🔧 tsup.config.ts 入口配置:');
  
  // 提取 entry 配置
  const entryMatch = configContent.match(/entry:\s*{([^}]+)}/s);
  if (entryMatch) {
    const entryLines = entryMatch[1].split('\n').filter(line => line.trim());
    entryLines.forEach(line => {
      if (line.includes(':')) {
        console.log(`  ${line.trim()}`);
      }
    });
  }
}

// 解析 package.json
if (fs.existsSync(packageJsonPath)) {
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
  console.log('\n📦 package.json exports 配置:');
  console.log(JSON.stringify(packageJson.exports, null, 2));
}

// 检查构建输出
console.log('\n📁 构建输出文件:');
const distPath = path.join(__dirname, '../packages/ui/dist');
if (fs.existsSync(distPath)) {
  const files = fs.readdirSync(distPath);
  const jsFiles = files.filter(file => file.endsWith('.js'));
  
  jsFiles.forEach(file => {
    const filePath = path.join(distPath, file);
    const stats = fs.statSync(filePath);
    const sizeKB = (stats.size / 1024).toFixed(2);
    console.log(`  ${file}: ${sizeKB} KB`);
  });
}

// 演示命名映射关系
console.log('\n🔗 命名映射关系:');

const mappings = [
  {
    entry: 'index: "src/index.ts"',
    output: 'dist/index.js',
    import: 'import { Button, Card, Code } from "@repo/ui"',
    description: '主入口，导入所有组件'
  },
  {
    entry: 'button: "src/button.tsx"',
    output: 'dist/button.js',
    import: 'import { Button } from "@repo/ui/button"',
    description: 'Button 组件独立入口'
  },
  {
    entry: 'card: "src/card.tsx"',
    output: 'dist/card.js',
    import: 'import { Card } from "@repo/ui/card"',
    description: 'Card 组件独立入口'
  },
  {
    entry: 'code: "src/code.tsx"',
    output: 'dist/code.js',
    import: 'import { Code } from "@repo/ui/code"',
    description: 'Code 组件独立入口'
  }
];

mappings.forEach((mapping, index) => {
  console.log(`\n${index + 1}. ${mapping.description}:`);
  console.log(`   入口配置: ${mapping.entry}`);
  console.log(`   输出文件: ${mapping.output}`);
  console.log(`   导入方式: ${mapping.import}`);
});

// 演示不同命名策略
console.log('\n🎯 不同命名策略对比:');

const strategies = [
  {
    name: '组件名命名（当前）',
    entry: {
      index: 'src/index.ts',
      button: 'src/button.tsx',
      card: 'src/card.tsx',
      code: 'src/code.tsx'
    },
    usage: [
      'import { Button } from "@repo/ui/button"',
      'import { Card } from "@repo/ui/card"'
    ],
    pros: ['直观易懂', '路径简洁', '符合习惯'],
    cons: ['组件多时可能混乱']
  },
  {
    name: '路径名命名',
    entry: {
      index: 'src/index.ts',
      'components/button': 'src/button.tsx',
      'components/card': 'src/card.tsx',
      'components/code': 'src/code.tsx'
    },
    usage: [
      'import { Button } from "@repo/ui/components/button"',
      'import { Card } from "@repo/ui/components/card"'
    ],
    pros: ['层次清晰', '便于管理', '扩展性好'],
    cons: ['路径较长', '不够简洁']
  },
  {
    name: '版本名命名',
    entry: {
      index: 'src/index.ts',
      'v1/button': 'src/button.tsx',
      'v1/card': 'src/card.tsx',
      'v2/button': 'src/button-v2.tsx'
    },
    usage: [
      'import { Button } from "@repo/ui/v1/button"',
      'import { ButtonV2 } from "@repo/ui/v2/button"'
    ],
    pros: ['支持版本管理', '向后兼容'],
    cons: ['复杂度高', '维护困难']
  }
];

strategies.forEach((strategy, index) => {
  console.log(`\n${index + 1}. ${strategy.name}:`);
  console.log(`   入口配置:`);
  Object.entries(strategy.entry).forEach(([key, value]) => {
    console.log(`     ${key}: "${value}"`);
  });
  console.log(`   使用方式:`);
  strategy.usage.forEach(usage => {
    console.log(`     ${usage}`);
  });
  console.log(`   优点: ${strategy.pros.join(', ')}`);
  console.log(`   缺点: ${strategy.cons.join(', ')}`);
});

// 实际建议
console.log('\n💡 实际建议:');

console.log('\n🎯 当前命名策略评估:');
console.log('  ✅ 使用组件名，直观易懂');
console.log('  ✅ 导入路径简洁：@repo/ui/button');
console.log('  ✅ 符合组件库的常见习惯');
console.log('  ✅ 便于维护和扩展');

console.log('\n🔧 命名最佳实践:');
console.log('  1. 保持命名一致性');
console.log('  2. 避免命名冲突');
console.log('  3. 考虑未来扩展');
console.log('  4. 符合项目习惯');

console.log('\n📝 命名规则:');
console.log('  - 使用小写字母');
console.log('  - 使用连字符分隔单词');
console.log('  - 避免特殊字符');
console.log('  - 保持简洁明了');

console.log('\n🚀 扩展建议:');
console.log('  如果未来组件增多，可以考虑:');
console.log('  1. 使用路径名：components/button');
console.log('  2. 使用分类名：ui/button, form/input');
console.log('  3. 使用版本名：v1/button, v2/button');

console.log('\n🎯 结论:');
console.log('  当前的组件名命名策略非常适合你的项目！');
console.log('  简洁、直观、易维护，建议保持。');

console.log('\n📚 更多信息:');
console.log('  - 入口命名详解: docs/entry-naming-explanation.md');
console.log('  - 多入口配置: docs/entry-points-explanation.md');
console.log('  - exports 字段: docs/exports-explanation.md'); 